package com.pms.privilege.permission.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.pms.privilege.permission.model.PermUserFuncModel;
import com.pms.privilege.permission.model.PermissionModel;
import com.pms.privilege.permission.service.PermissionService;
import com.pms.utils.CalPermNo;
import com.pms.utils.CookieManage;
@Controller
public class PermissionController {
	@Autowired
	private PermissionService ps;
	/**
	 * 打开权限首页
	 * @param req
	 * @param mm
	 * @return
	 */
	@RequestMapping(value="permindex.pms")
	public String index(HttpServletRequest req,ModelMap mm){
		CookieManage cm = new CookieManage();
		Map<String, Cookie> map = new HashMap<String, Cookie>();
		map = cm.getCookies(req);
		if (map!=null&&map.size()>0&&map.containsKey("permission")) {
			String permission = map.get("permission").getValue();
			if (permission != null && "yes".equals(permission)) {
				return "permission/permindex";
			}
		}
		return "redirect:login.pms";
	}
	/**
	 * 查询权限列表
	 * @param req
	 * @param resp
	 */
	@RequestMapping(value="perminfo.pms",method=RequestMethod.POST)
	public void permInfo(HttpServletRequest req,HttpServletResponse resp){
		try{
			String func_id=req.getParameter("func_id");
			JSONArray userArray=new JSONArray();
			if(func_id!=null&&!"".equals(func_id)){
				List<PermUserFuncModel> list=new ArrayList<PermUserFuncModel>();
				list=ps.permListService(Long.parseLong(func_id),0,10);
				if(list.size()>0){
					for(PermUserFuncModel pufm:list){
						JSONObject userinfo=new JSONObject(pufm);
						String[] perms=CalPermNo.getInstance().calPerm(pufm.getPerm_no());
						userinfo.put("perm_1",perms[0]);
						userinfo.put("perm_2",perms[1]);
						userinfo.put("perm_4",perms[2]);
						userArray.put(userinfo);
					}
				}
			}
			
			resp.setContentType("application/json; charset=UTF-8");
			resp.getWriter().print(userArray);
			resp.getWriter().close();
		}catch(IOException e){
			e.printStackTrace();
		}	
	}
	
	/**
	 * 新增权限
	 * @param perm_id
	 * @param resp
	 */
	@RequestMapping(value="perminsert.pms",method=RequestMethod.POST)
	public void insertPerm(HttpServletRequest req,HttpServletResponse resp){
		try{
			String func_id=req.getParameter("func_id");
			String user_id=req.getParameter("user_id");
			String perm_no[]=req.getParameterValues("perm_no[]");
			JSONObject json=new JSONObject();
			if(func_id!=null&&!"".equals(func_id)&&user_id!=null&&!"".equals(user_id)&&perm_no!=null&&!"".equals(perm_no)){
				int perm=0;
				for(int i=0;i<perm_no.length;i++){
					perm+=Integer.parseInt(perm_no[i]);
				}
				PermissionModel pm=new PermissionModel();
				pm.setPerm_id("f"+func_id+"u"+user_id);
				pm.setFunc_id(Long.parseLong(func_id));
				pm.setUser_id(Long.parseLong(user_id));
				pm.setPerm_no(perm);
				json.put("msg", ps.insertPermService(pm));
			}
			resp.setContentType("application/json; charset=UTF-8");
			resp.getWriter().print(json);
			resp.getWriter().close();
		}catch(IOException e){
			e.printStackTrace();
		}
	}
	
	/**
	 * 删除权限
	 * @param perm_id
	 * @param resp
	 */
	@RequestMapping(value="permdelete.pms",method=RequestMethod.POST)
	public void deletePerm(String perm_id,HttpServletResponse resp){
		try{
			JSONObject json=new JSONObject();
			json.put("msg", ps.deletePermService(perm_id));
			resp.setContentType("application/json; charset=UTF-8");
			resp.getWriter().print(json);
			resp.getWriter().close();
		}catch(IOException e){
			e.printStackTrace();
		}
	}
	/**
	 * 修改权限
	 * @param perm_id
	 * @param resp
	 */
	@RequestMapping(value="permupdate.pms",method=RequestMethod.POST)
	public void updatePerm(HttpServletRequest req,HttpServletResponse resp){
		try{
			String perm_id=req.getParameter("perm_id");
			String[] perm_no=req.getParameterValues("perm_no[]");
			JSONObject json=new JSONObject();
			if(perm_id!=null&&!"".equals(perm_id)){
				if(perm_no!=null&&!"".equals(perm_no)){
					int perm=0;
					for(int i=0;i<perm_no.length;i++){
						perm+=Integer.parseInt(perm_no[i]);
					}
					PermissionModel pm=new PermissionModel();
					pm.setPerm_id(perm_id);
					pm.setPerm_no(perm);
					json.put("msg", ps.updatePermService(pm));
				}else{
					json.put("msg", ps.deletePermService(perm_id));
				}
				
			}
			resp.setContentType("application/json; charset=UTF-8");
			resp.getWriter().print(json);
			resp.getWriter().close();
		}catch(IOException e){
			e.printStackTrace();
		}
	}

}
